package com.EWS.controller;

import com.EWS.model.UserDto;
import com.EWS.service.UserService;
import com.EWS.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

// 与前端交互在学习记录页面进行各种操作
@WebServlet("/StudyServlet")
public class StudyServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    // 转换单词系统页面的信息
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        // 通过前端登录时提交的会话信息获取此时登录的用户名
        HttpSession session = req.getSession(false); // 不创建新会话，过去前端会话信息
        String userName = (String) session.getAttribute("userName");
        // 获取前端操作的指令和要操作的单词
        String opsCode = req.getParameter("opsCode");
        String word = req.getParameter("word");
        // 点击认识
        if(opsCode.equals("know")){
            // 该单词的学习次数加1，并更新学习时间
            int result = userService.updateStudy(userName,word);
            // 刷新学习页面
            List<UserDto> userDtoList = userService.getUserStudy(userName,"");
            // 将获取到的信息送到前端页面
            req.setAttribute("userDtoList",userDtoList);
            req.getRequestDispatcher("study.jsp").forward(req,resp);
        }
        // 点击不认识
        if(opsCode.equals("noKnow")){
            /** 在这次界面中不显示这个单词，在下次点学习的时候再出现*/
            // 刷新学习页面
            List<UserDto> userDtoList = userService.getUserStudy(userName,"");
            // 将获取到的信息送到前端页面
            req.setAttribute("userDtoList",userDtoList);
            req.getRequestDispatcher("study.jsp").forward(req,resp);
        }

    }
}
